/************************************************************************

								Heroes IV
					  Copyright 2001, The 3DO Company

	------------------------------------------------------------------
						spell_ai_importance_map.cpp

	$Header: $

	$NoKeywords: $

 ************************************************************************/

#include "precompiled.h"
#pragma warning( disable: 4786 ) // Disable the ever so helpful "identifier was truncated to '255' characters" warning

#include "spell_ai_importance_map.h"

#include "ai_importance.h"
#include "spell.h"
#include "sized_int_types.h"
#include "streambuf_operators.h"

namespace
{
	t_uint16 const k_current_version = 0;	
}

// --------------------------------------------------------------------------
// Read in a list of spells with non-standard importance
// --------------------------------------------------------------------------
bool t_spell_ai_importance_map::read( std::streambuf & stream )
{
	t_uint16 format_version = get< t_uint16 >( stream );

	if ( format_version > k_current_version )
		return false;

	t_uint16 size = get< t_uint16 >( stream );

	while( size )
	{
		t_spell spell = t_spell( get< t_uint16 >( stream ) );

		if ( spell < 0 || spell >= k_spell_count )
			return false;

		t_ai_importance import = t_ai_importance( get< t_uint8 >( stream ) );

		if ( import < 0 || import >= k_ai_importance_count )
			return false;

		if ( import == k_ai_importance_normal )
			return false; // Map is only for NON-STANDARD importance

		std::pair< iterator, bool > result = insert( value_type( spell, import ) );
		if ( !result.second )
			return false;

		size--;
	}

	return true;
}

// --------------------------------------------------------------------------
// Write out a list of spells with non-standard importance
// --------------------------------------------------------------------------
bool t_spell_ai_importance_map::write( std::streambuf & stream ) const
{
	put< t_uint16 >( stream, k_current_version );

	put< t_uint16 >( stream, size() );

	const_iterator it;

	for ( it = begin(); it != end(); it++ )
	{
		put< t_uint16 >( stream, it->first );
		put< t_uint8 >( stream, it->second );
	}

	return true;
}
